<template>
  <div class="dropdown" ref="dropdownRef">
    <a href="#" class="btn btn-outline-light my-2 dropdown-toggle" @click.prevent="toggle()">{{ title }}</a>
    <ul class="dropdown-menu" :style="{ display: 'block' }" v-if="isOpen">
      <slot></slot>
    </ul>
  </div>
</template>

<script lang='ts'>
import { defineComponent, ref } from 'vue'
import useClickOutside from '../hooks/useClickOutside'

export default defineComponent({
  name: 'Dropdown',
  props: {
    title: {
      type: String,
      required: true
    }
  },
  setup () {
    const dropdownRef = ref<HTMLElement | null>(null)

    const isOpen = ref(false)

    const toggle = () => {
      isOpen.value = !isOpen.value
    }

    useClickOutside(dropdownRef, (flag) => {
      if (!flag && isOpen.value) {
        isOpen.value = false
      }
    })

    return {
      dropdownRef,
      isOpen,
      toggle
    }
  }
})
</script>
